from django.shortcuts import render, HttpResponse
from django.http.request import HttpRequest
from django.views import View
from .utils import dec_data, enc_data
from .models import Card
from django.utils import timezone


# Create your views here.


class VerifyCard(View):

    def post(self, request: HttpRequest, *args, **kwargs):
        data = dec_data(request.body)
        card = data.get("card")
        data = {"success": False, }
        if not card:
            data["msg"] = '请求出错!'
            data = enc_data(data)
            try:
                return HttpResponse(content=data)
            except:
                return HttpResponse(content=data)
        else:
            try:
                card_obj = Card.objects.get(card=card)
                if card_obj.online_devices > card_obj.devices:
                    data['msg'] = '设备上限!'
                    data = enc_data(data)
                    return HttpResponse(content=data)
                if card_obj.end_time > timezone.now():
                    data['msg'] = '卡密过期!'
                    data = enc_data(data)

                    return HttpResponse(content=data)
                else:
                    data["success"] = True
                    data['msg'] = '成功!'
                    card_obj.online_devices = card_obj.online_devices + 1
                    card_obj.save()
                    data = enc_data(data)

                    return HttpResponse(content=data)
            except Card.DoesNotExist:
                data['msg'] = '卡密未找到!'
                data = enc_data(data)
                return HttpResponse(content=data)


class DecreaseCard(View):

    def post(self, request: HttpRequest, *args, **kwargs):
        data = dec_data(request.body)
        card = data.get("card")
        data = {"success": False, }
        if not card:
            data["msg"] = '请求出错!'
            try:
                data = enc_data(data)
                data = enc_data(data)

                return HttpResponse(content=data)
            except:
                data = enc_data(data)

                return HttpResponse(content=data)
        else:
            try:
                card_obj = Card.objects.get(card=card)
                if card_obj.end_time > timezone.now():
                    data['msg'] = '卡密过期!'
                    data = enc_data(data)

                    return HttpResponse(content=data)
                else:
                    data["success"] = True
                    if card_obj.online_devices <=0:
                        card_obj.online_devices = 0
                        card_obj.save()
                        data['msg'] = '成功!'
                        data = enc_data(data)

                        return HttpResponse(content=data)
                    else:
                        card_obj.online_devices = card_obj.online_devices-1
                        card_obj.save()
                        data['msg'] = '成功!'
                        data = enc_data(data)

                        return HttpResponse(content=data)
            except Card.DoesNotExist:
                data['msg'] = '卡密未找到!'
                data = enc_data(data)

                return HttpResponse(content=data)
